<template>
  <div class="chart-view">
    <div v-show="dataLen" ref="lineChartRef" class="chart-box"></div>
    <Empty v-show="!dataLen" />
  </div>
</template>

<script lang="ts" setup>
import { ref, nextTick, onMounted, onBeforeUnmount } from "vue";
import type { TestResultItem } from "../../api/test/index.d";
import * as echarts from "echarts";
import useConfig from "../hooks/useConfig";
let lineChart = null as any;

const props = withDefaults(defineProps<{ dataLen: number; caseName?: string }>(), {
  dataLen: 0,
  caseName: "",
});

const lineChartRef = ref<HTMLElement>();
onMounted(async () => {
  nextTick(() => {
    initEchart();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", chartResize);
});
const initEchart = () => {
  lineChart = echarts.init(lineChartRef.value);
  // lineChart.setOption(getJson().json);
  window.addEventListener("resize", chartResize);
};
const chartResize = () => {
  lineChart.resize();
};
const drawChart = (data: TestResultItem[], treeRootName: string) => {
  lineChart.clear();
  lineChart.setOption(useConfig().getJson(props.caseName, data, treeRootName).json);
  chartResize();
};

defineExpose({ chartResize, drawChart });
</script>
<style scoped lang="scss">
.chart-view {
  width: 100%;
  height: 100%;
  border-bottom: 1px solid #eaeaea;
  position: relative;
  .chart-box {
    width: 100%;
    height: 100%;
  }
  .el-empty {
    width: 100%;
    height: 100%;
  }
}
</style>
<style lang="scss">
.test-project-tooltip-box {
  background: linear-gradient(321deg, rgba(253, 254, 255, 0.9) 0%, rgba(244, 247, 252, 0.9) 100%) !important;
  .tit {
    font-weight: 600;
  }
  .list {
    background: rgba(255, 255, 255, 0.8);
    box-shadow: 6px 0px 20px 0px rgba(34, 87, 188, 0.1);
    border-radius: 4px;
    margin-top: 4px;
    padding: 6px 10px;
    min-width: 142px;
    .name-val {
      display: flex;
      align-items: center;
      justify-content: space-between;
      &:not(:last-child) {
        margin-bottom: 4px;
      }
      .name {
        color: #4e5969;
      }
      .val {
        margin-left: 15px;
      }
      i {
        display: inline-block;
        width: 10px;
        height: 10px;
        border-radius: 2px;
        margin-right: 5px;
      }
    }
  }
}
</style>
../hooks/index